<template>
  <div class="sxpcwlkj-store_advertising_location-container layout-padding">
    <el-card shadow="hover" class="layout-padding-auto">
      <!-- 功能栏 -->
      <el-form :inline="true" :model="state.queryForm" class="sxpcwlkj-store_advertising_location-search mb15">
        <el-form-item>
          <el-button size="default" @click="getTableData" type="primary" class="ml10">
            <el-icon>
              <ele-Search/>
            </el-icon>
            查询
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="default" type="success" class="ml10" @click="onCURD('add')">
            <el-icon>
              <ele-FolderAdd/>
            </el-icon>
            新增
          </el-button>
        </el-form-item>
      </el-form>
      <!-- Table -->
      <el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%"
                @selection-change="selectionChangeHandle">
        <el-table-column v-if="false" type="selection" header-align="center" align="center"
                         width="50"></el-table-column>
        <el-table-column v-if="false" prop="id" label="ID" header-align="center" align="center"></el-table-column>
        <el-table-column prop="name" label="广告位名称" header-align="center" align="center"></el-table-column>
        <el-table-column prop="code" label="广告位编码" header-align="center" align="center"></el-table-column>
        <el-table-column prop="width" label="宽度" header-align="center" align="center">
          <template #default="scope">
            {{ scope.row.width }}px
          </template>
        </el-table-column>
        <el-table-column prop="height" label="高度" header-align="center" align="center">
          <template #default="scope">
            {{ scope.row.height }}px
          </template>
        </el-table-column>
        <el-table-column prop="maxNum" label="最大显示数量" header-align="center" align="center">
          <template #default="scope">
            {{ scope.row.maxNum }}个
          </template>
        </el-table-column>

        <el-table-column prop="sort" label="排序" header-align="center" align="center"></el-table-column>
        <el-table-column prop="remark" label="备注" header-align="center" align="center"></el-table-column>
        <fast-table-column prop="status" label="状态" dict-type="SYS_STATE"></fast-table-column>
        <el-table-column label="操作" width="100">
          <template #default="scope">
            <Auth :value="'sxpcwlkj:store_advertising_location:edit'">
              <el-button size="small" text type="primary" @click="onCURD('edit', scope.row)">修改</el-button>
            </Auth>
            <Auth :value="'sxpcwlkj:store_advertising_location:delete'">
              <el-button size="small" text type="primary" @click="onCURD('del',scope.row)">删除</el-button>
            </Auth>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
                     :pager-count="5" :page-sizes="[10, 20, 30]" v-model:current-page="state.tableData.param.pageNum"
                     background
                     v-model:page-size="state.tableData.param.pageSize" layout="total, sizes, prev, pager, next, jumper"
                     :total="state.tableData.total">
      </el-pagination>
    </el-card>
    <Store_advertising_locationDialog ref="store_advertising_locationDialogRef" @refresh="formSubmit"/>
  </div>
</template>

<script setup lang="ts" name="sxpcwlkjStore_advertising_location">
// 引入组件
import FastSelect from "/@/components/fast-select/src/fast-select.vue";
import FastTableColumn from "/@/components/fast-table-column/src/fast-table-column.vue";
import FastRadioGroup from "/@/components/fast-radio-group/src/fast-radio-group.vue";

import {defineAsyncComponent, reactive, onMounted, ref} from 'vue';
import {ElMessageBox, ElMessage} from 'element-plus';
import {isEmpty} from '/@/utils/mms';
import {store_advertising_locationApi} from '/@/api/sxpcwlkj/store_advertising_location/index';
// 定义变量内容
// 引入 api 请求接口
const baseApi = store_advertising_locationApi();
const store_advertising_locationDialogRef = ref();
const Store_advertising_locationDialog = defineAsyncComponent(() => import('/@/views/sxpcwlkj/store_advertising_location/dialog.vue'));
// 权限
const Auth = defineAsyncComponent(() => import('/@/components/auth/auth.vue'));
const Auths = defineAsyncComponent(() => import('/@/components/auth/auths.vue'));
const AuthAll = defineAsyncComponent(() => import('/@/components/auth/authAll.vue'));

const state = reactive<Store_advertising_locationState>({
  queryForm: {},
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10
    },
  },
});

// 初始化表格数据
const getTableData = () => {
  state.tableData.loading = true;
  baseApi.list(state.tableData.param).then(res => {
    state.tableData.data = res.rows;
    state.tableData.total = res.total;
    state.tableData.loading = false;
  }).catch(async err => {
    ElMessage.warning(err);
  }).finally(() => {
  })
};

//选择
const selectionChangeHandle = () => {
  //略
}
// 打开修改用户弹窗
const onCURD = (type: string, row: RowStore_advertising_locationType) => {
  if (type === 'add') {
    store_advertising_locationDialogRef.value.openDialog(type);
    return false;
  }
  if (type === 'del') {
    ElMessageBox.confirm(`此操作将永久删除吗?`, '提示', {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(() => {
      baseApi.delete(row.id).then(res => {
        getTableData();
        ElMessage.success('删除成功');
      }).catch(async err => {
        ElMessage.warning(err);
      }).finally(() => {
      })
    }).catch(() => {
    });
    return false;
  }
  baseApi.query(row.id).then(res => {
    store_advertising_locationDialogRef.value.openDialog(type, res.data);
  }).catch(async err => {
    ElMessage.warning(err);
  }).finally(() => {
  })
};

// 接收子组件传值
const formSubmit = (row: RowStore_advertising_locationType) => {
  if (isEmpty(row.id)) {
    //新增
    baseApi.insert(row).then(row => {
      store_advertising_locationDialogRef.value.closeDialog();
      ElMessage.success(row.msg)
      setTimeout(() => {
        getTableData();
      }, 1000)
    }).catch(async err => {
      ElMessage.warning(err);
    }).finally(() => {
    })
  } else {
    //更新
    baseApi.edit(row).then(row => {
      store_advertising_locationDialogRef.value.closeDialog();
      ElMessage.success(row.msg)
      setTimeout(() => {
        getTableData();
      }, 1000)
    }).catch(async err => {
      ElMessage.warning(err);
    }).finally(() => {
    })
  }
}
// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  getTableData();
});
</script>

<style scoped lang="scss">

</style>
